package com.yuji.aacar.controller.order.coupon;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.bean.response.ValidatorCouponResponse;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.canceledit.CancelEditText;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <li>Package:com.yuji.aacar.controller.order.coupon</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/9</li>
 * <li>Description:  优惠券 </li>
 */

public class CouponActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.coupon_number)
    CancelEditText couponNumber;
    @BindView(R.id.coupon_password)
    CancelEditText couponPassword;
    @BindView(R.id.order_coupon_btn)
    Button orderCouponBtn;

    private String couponNoStr;
    private String couponPasswordStr;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_coupon);
        ButterKnife.bind(this);
        initTitle(true, "使用优惠券", null);
        super.onCreate(savedInstanceState);

        couponNumber.setText("1000101-100");
        couponPassword.setText("1234");
    }

    @OnClick({R.id.coupon_number, R.id.coupon_password, R.id.order_coupon_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.coupon_number:
                break;
            case R.id.coupon_password:
                break;
            case R.id.order_coupon_btn:
                if (isLegal()){
                    requestParams = BaseServiceParams.validatorCoupon(couponNoStr,couponPasswordStr);
                    doTask(BaseServiceMediator.VALIDATOR_COUPON);
                }
                break;
        }
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.VALIDATOR_COUPON_CODE) {
            ValidatorCouponResponse submitResponse = (ValidatorCouponResponse) response.getResponse();
            if (submitResponse.getFlag().contentEquals("1")){
                CoreEventCenter.postMessage(EventConst.COUPON,couponNoStr+":"+submitResponse.getCouponAmount());
                finish();
            } else {
                showToast(submitResponse.getInfo());
            }
        }
    }


    private boolean isLegal() {
        couponNoStr = couponNumber.getText().toString();
        couponPasswordStr = couponPassword.getText().toString();


        if (TextUtils.isEmpty(couponNoStr)){
            showToast("优惠券码不能为空");
            return false;
        }
        if (TextUtils.isEmpty(couponPasswordStr)){
            showToast("优惠券码密码不能为空");
            return false;
        }
        return true;
    }

    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
        ValidatorCouponResponse response1 = (ValidatorCouponResponse) response.getResponse();
        showToast(response1.getInfo());
    }
}
